<?php
    if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])){exit();}
?>
<style type="text/css">
    label { display: block; }
    #login_form fieldset {
        border: 0px;
    }
</style>

<div class="modal" id="login_overlay">
    <h2 class="login">JWV Stock Login</h2>
    
    <form id="login_form" name="login_form" action="" onSubmit="return false;">
        <fieldset>
            <p>
                Please login to your account.
            </p>
            
            <label for="login_username">Username:</label>
            <input type="text" id="login_username" name="login_username" />
            <br/><br/>
            <label for="login_password">Password:</label>
            <input type="password" id="login_password" name="login_password" />
            <br/><br/>
            <div id="login_result" style="display:none;"></div>
            <br>
            <center><input type="submit" class="button blue" value="Login" /></center>
        </fieldset>
    </form>
</div>


<script type="text/javascript">
    login_overlay = $("#login_overlay").overlay(
        {
            closeOnClick: false,
            closeOnEsc: false,
            load: <?php if (isLoggedIn()) echo "false"; else echo "true"; ?>,
            speed: 'fast',
            fixed: false,
            mask: {
                color: '#ebecff',
                loadSpeed: 200,
                opacity: 0.9
            }
        }
    );
    $("#login_overlay form").submit(function(e) {
        var resp = $("#login_result");
        resp.hide(100).delay(50);
        var username = $("#login_username").val().trim();
        var password = $("#login_password").val().trim();
        if (username != "" && password != "") {
            resp.attr("class", "info");
            resp.text("Logging in ...");
            resp.show(100).delay(50);
            $.ajax({
                type: "POST",
                url: "backend/login.php",
                dataType: "json",
                data: { user: username, pass: password },
                success: function(data) {
                    if (data["Success"] == 0) {
                        resp.attr("class", "error");
                        resp.text("Incorrect username or password!");
                        resp.show(100);
                    } else {
                        $("#login_username").val("");
                        $("#login_password").val("");
                        login_overlay.eq(0).overlay().close();
                        var u = data["User"];
                        $("#wrap").show();
                        $("#footer").show();
                        $("#0").text(u["FirstName"] + " " + u["LastName"] + " (" + u["UserName"] + ")");
                        $("#1").text(u["UserID"]);
                        $("#2").text(u["UserName"]);
                        $("#3").text(u["FirstName"] + " " + u["LastName"]);
                        $("#4").text(u["UserType"]);
                        $("#5").text(u["Address"]);
                        $("#6").text(u["City"]);
                        $("#7").text(u["State"]);
                        $("#8").text(u["ZipCode"]);
                        $("#9").text(u["TelephoneNumber"]);
                        
                        doStockLookup("IBM, F, GM"); //Load default stocks
                        $("#customeraccountwidget").hide();
                        $("#rec_stock_div").hide();
                        //$("#customerorderwidget").hide();
                        if (u["UserType"] == "Customer") {
                            $("#customeraccountwidget").show();
                            loadAccountInfo();
                            $("#rec_stock_div").show();
                            suggestStock();
                            //$("#customerorderwidget").show();
                            //loadOrderInfo();
                        }
                        
                        $("#create_order_widget").hide();
                        
                        $("#NewUserButton").hide();
                        $("#search_user_widget").hide();
                        $("#administrativewidget").hide();
                        $("#administration_manager").hide();
                        if (u["UserType"] == "Representative" || u["UserType"] == "Manager") {
                            $("#create_order_widget").show();
                            $("#NewUserButton").show();
                            $("#search_user_widget").show();
                            $("#administrativewidget").show();
                            if (u["UserType"] == "Manager")
                                $("#administration_manager").show();
                        }
                    }
                },                    
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    resp.attr("class", "error");
                    resp.text("Something went wrong! Please try again later.");
                    resp.show(100);
                }
            });
            
        } else {
            resp.attr("class", "error");
            resp.text("Please enter your username and password.")
            resp.show(100);
        }
        
        return e.preventDefault();
    });
</script>